/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package DAO;

import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

/**
 *
 * @author sio
 */
public class DAO {
    private static final String url ="http://gaemedecins.appspot.com/Controleur/medParDep/listeDep";
    public static List<String>getLesDeps(){
        List<String>Deps = new ArrayList();
        try{
            URL UrlDeps = new URL(url);
            Document doc;
            DocumentBuilderFactory dbfDeps = DocumentBuilderFactory.newInstance();
            DocumentBuilder dbDeps = dbfDeps.newDocumentBuilder();
            doc=dbDeps.parse(UrlDeps.openStream());
            Element Racine = doc.getDocumentElement();
            
            NodeList listeDep= Racine.getElementsByTagName("Departement");
            
            for(int i = 0; i< listeDep.getLength(); i++){
                Node departement = listeDep.item(i);
                NodeList lesProprietes = departement.getChildNodes();
                for (int j = 0; j <lesProprietes.getLength(); j++){
                    if (lesProprietes.item(j).getNodeName().equals("num")){
                        Deps.add(lesProprietes.item(j).getTextContent().trim());
                        break;
                    }
                }
            }
            
        }
        catch(Exception ex){
            System.out.println("");
        }
        return Deps;
        
    }
    private static final String urlun = "http://gaemedecins.appspot.com/Controleur/medParDep/listeMed/";
    public static List<String>getLesMeds(String leDep){
        String monUrl = urlun + leDep;
        List<String>Meds = new ArrayList();
        try{
            URL UrlDeps = new URL(monUrl);
            Document doc;
            DocumentBuilderFactory dbfDeps = DocumentBuilderFactory.newInstance();
            DocumentBuilder dbDeps = dbfDeps.newDocumentBuilder();
            doc=dbDeps.parse(UrlDeps.openStream());
            Element Racine = doc.getDocumentElement();
            NodeList listeMeds = Racine.getElementsByTagName("Medecin");
            for(int i = 0; i < listeMeds.getLength(); i++){
                Node medecin = listeMeds.item(i);
                NodeList lesMedecins = medecin.getChildNodes();
            for (int j = 0; j < lesMedecins.getLength(); j++){
                if(lesMedecins.item(j).getNodeName().equals("nom")){
                    Meds.add(lesMedecins.item(j).getTextContent().trim());
                    break;
                }
            }}
        
        
        
        }
        catch(Exception ex){
            
        }
        return Meds;
        
    }

    
}
